package com.thrd.taiexhibition.fragement;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.thrd.basicframework.bean.BaseFragment;
import com.thrd.basicframework.bean.BaseRecyclerAdapter;
import com.thrd.basicframework.ui.defaultview.LoadingState;
import com.thrd.basicframework.ui.recyclerview.SpacesItemDecoration;
import com.thrd.taiexhibition.BaseApplication;
import com.thrd.taiexhibition.R;
import com.thrd.taiexhibition.adapter.CityHotSpotLeftAdapter;
import com.thrd.taiexhibition.adapter.CityHotSpotRightAdapter;
import com.thrd.taiexhibition.bean.BeanHotActivityList;
import com.thrd.taiexhibition.bean.BeanHotActivty;
import com.thrd.taiexhibition.bean.BeanHotArticle;
import com.thrd.taiexhibition.bean.CityEvent;
import com.thrd.taiexhibition.presener.CityHotSpotPresener;
import com.thrd.taiexhibition.utils.DialogUtils;
import com.thrd.taiexhibition.utils.JumpUtils;
import com.thrd.taiexhibition.view.CityHotSpotView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * @author: ${唐杰}
 * @date: 2021/3/25
 */
public class CityHotSpotFragment extends BaseFragment implements CityHotSpotView {
    private CityHotSpotPresener mCityFocusPresener;
    private RecyclerView mRecyclerViewLeft;
    private RecyclerView mRecyclerViewRight;
    private CityHotSpotLeftAdapter mCityHotSpotLeftAdapter;
    private CityHotSpotRightAdapter mCityHotSpotRightAdapter;
    private int cityId;
    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_hot_spot;
    }

    @Override
    protected void setUpView(View mContentView) {
        mCityFocusPresener = new CityHotSpotPresener(this);
        mRecyclerViewLeft = mContentView.findViewById(R.id.rv_recyclerview_left);
        mRecyclerViewLeft.setLayoutManager(new LinearLayoutManager(getMContext()));
        mRecyclerViewRight = mContentView.findViewById(R.id.rv_recyclerview_right);
//        LinearLayoutManager mLayoutManager=new LinearLayoutManager(getMContext());
//        mLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        mRecyclerViewRight.setLayoutManager(new GridLayoutManager(getContext(),3));
        SpacesItemDecoration mSpacesItemDecoration =  new SpacesItemDecoration(0,0,getMContext().getResources().getDimensionPixelSize(R.dimen.qb_px_10),getMContext().getResources().getDimensionPixelSize(R.dimen.qb_px_10));
        mSpacesItemDecoration.setOneLeft(getMContext().getResources().getDimensionPixelSize(R.dimen.qb_px_10),3);
        mRecyclerViewRight.addItemDecoration(mSpacesItemDecoration);
    }

    @Override
    protected void setUpData() {
        mCityHotSpotLeftAdapter = new CityHotSpotLeftAdapter(getMContext(),null);
        mRecyclerViewLeft.setAdapter(mCityHotSpotLeftAdapter);


        mCityHotSpotLeftAdapter.setItemClick(new BaseRecyclerAdapter.ItemClick<BeanHotArticle>() {
            @Override
            public void onItemClick(int p, BeanHotArticle e) {
                JumpUtils.JumpUtils(2,e.getArticleId());
            }
        });

        mCityHotSpotRightAdapter = new CityHotSpotRightAdapter(getMContext(),null);
        mRecyclerViewRight.setAdapter(mCityHotSpotRightAdapter);

        mCityHotSpotRightAdapter.setOnItemClickListener(new CityHotSpotRightAdapter.OnItemClickListener<BeanHotActivty>() {
            @Override
            public void onImageClickListener(View view, BeanHotActivty mBeanHotActivty) {
                DialogUtils.showQRDialog(getActivity(),null,mBeanHotActivty.getHotActivityQrCode());
            }
        });
    }

    @Override
    public void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();
        BaseApplication mBaseApplication = (BaseApplication) getActivity().getApplication();
        if(mCityFocusPresener!=null&&mBeanHotActivityList==null&&mBaseApplication!=null){
            mCityFocusPresener.getCityFocusData(cityId= (int) mBaseApplication.getCityId());
        }else if(mBeanHotActivityList!=null){
            onSuccess(mBeanHotActivityList);
        }
    }

    @Override
    public void onError(String error, int type) {
        getContentView().setState(LoadingState.STATE_ERROR);
    }

    private BeanHotActivityList mBeanHotActivityList;
    @Override
    public void onSuccess(BeanHotActivityList mBeanHotActivityList) {
        this.mBeanHotActivityList=mBeanHotActivityList;
        mCityHotSpotLeftAdapter.refresh(mBeanHotActivityList.getHotArticleList());
        mCityHotSpotRightAdapter.refresh(mBeanHotActivityList.getHotActivityList());
        getContentView().setState(LoadingState.STATE_SUCCESS);
    }

    @Override
    protected void onRecoveryBundle(Bundle savedInstanceState) {
        super.onRecoveryBundle(savedInstanceState);
        BaseApplication mBaseApplication = (BaseApplication) getActivity().getApplication();
        if(mBaseApplication!=null&&savedInstanceState!=null){
            long cityID=savedInstanceState.getLong("cityId");
            if(mBaseApplication.getCityId()==cityID){
                mBeanHotActivityList= (BeanHotActivityList) savedInstanceState.getSerializable("BeanHotActivityList");
            }
        }

    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("BeanHotActivityList",  mBeanHotActivityList);


    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        mBeanHotActivityList=null;
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void switchCity(CityEvent mCityEvent){
        if(mCityEvent!=null&&mCityFocusPresener!=null){
            mBeanHotActivityList=null;
            mCityFocusPresener.getCityFocusData( cityId= (int) mCityEvent.getCityId());
        }

    }

}
